package bb.lanxing.activity.sport;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.github.mikephil.charting.utils.Utils;
import com.tencent.connect.common.Constants;
import com.umeng.analytics.pro.d;

import java.io.File;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.adapter.sport.LocalWorkoutAdapter;
import bb.lanxing.databinding.ActivityLocalWorkoutBinding;
import bb.lanxing.devices.sync.DeviceSyncHelper;
import bb.lanxing.model.database.Workout;
import bb.lanxing.mvvm.base.BaseDBActivity;
import bb.lanxing.service.SyncTaskService;
import bb.lanxing.util.Log;
import bb.lanxing.util.ShareLogUtil;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.DefaultConstructorMarker;


public class LocalWorkoutActivity extends BaseDBActivity<ActivityLocalWorkoutBinding> {
    public static Companion Companion = new Companion(null);
    private int layoutId;
    private LocalWorkoutAdapter localWorkoutAdapter;
    private BroadcastReceiver workoutSyncBroadcastReceiver;

    public LocalWorkoutActivity() {
        this(0, 1, null);
    }

    public LocalWorkoutActivity(int layoutId) {
        this.layoutId = layoutId;
        this.workoutSyncBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action;
                String str;

                action = intent.getAction();
                if (action == null) {
                    return;
                }

                long longExtra = intent.getLongExtra(SyncTaskService.EXTRA_WORKOUT_ID, 0L);
                long intExtra = intent.getIntExtra(SyncTaskService.EXTRA_TASK_ID, 0);
                boolean booleanExtra = intent.getBooleanExtra("success", false);
                double doubleExtra = intent.getDoubleExtra("progress", Utils.DOUBLE_EPSILON);

                if (SyncTaskService.TASK_SYNC_WORKOUT == intExtra) {

                    switch (action) {
                        case SyncTaskService.ACTION_SYNC_ALREADY_SYNC://-1799768263:
                            str = SyncTaskService.ACTION_SYNC_ALREADY_SYNC;

                            break;
                        case SyncTaskService.ACTION_SYNC_ITEM_RESULT://-1385546798:


                            if (booleanExtra) {
                                LocalWorkoutActivity.this.onSuccess(longExtra);
                                return;
                            }
                            String stringExtra = intent.getStringExtra(d.O);
                            if (stringExtra == null) {
                                stringExtra = "";
                            }
                            LocalWorkoutActivity.this.onFailed(longExtra, stringExtra);
                            return;
                        case SyncTaskService.ACTION_SYNC_ITEM_PROGRESS://-735896414:

                            onUpdate(longExtra, doubleExtra);

                            return;
                        case SyncTaskService.ACTION_SYNC_WORKOUTS_FINISH://-688303239:
                            str = SyncTaskService.ACTION_SYNC_WORKOUTS_FINISH;
                            if (booleanExtra) {
                                onSuccess(longExtra);
                            } else {
                                stringExtra = intent.getStringExtra(d.O);
                                if (stringExtra == null) {
                                    stringExtra = "";
                                }
                                onFailed(longExtra, stringExtra);
                            }

                            break;
                        case SyncTaskService.ACTION_SYNC_WORKOUTS_START://137422795:
                            str = SyncTaskService.ACTION_SYNC_WORKOUTS_START;

                            break;
                        default:
                            return;
                    }

                    Log.d(str);
                }
            }
        };
        this.localWorkoutAdapter = new LocalWorkoutAdapter();
    }

    public LocalWorkoutActivity(int layoutId, int i2, DefaultConstructorMarker defaultConstructorMarker) {
        this((i2 & 1) != 0 ? R.layout.activity_local_workout : layoutId);
    }

    @Override
    public int getLayoutId() {
        return this.layoutId;
    }

    public BroadcastReceiver getWorkoutSyncBroadcastReceiver() {
        return this.workoutSyncBroadcastReceiver;
    }

    public LocalWorkoutAdapter getLocalWorkoutAdapter() {
        return this.localWorkoutAdapter;
    }

    @Override
    public void initView(ActivityLocalWorkoutBinding binding) {

        setTitle(R.string.st_local_workout);
        setupActionBar(true);
        binding.recyclerView.setAdapter(this.localWorkoutAdapter);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this.context));
        binding.recyclerView.setItemAnimator(new DefaultItemAnimator());
        BroadcastReceiver broadcastReceiver = this.workoutSyncBroadcastReceiver;
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(SyncTaskService.ACTION_SYNC_ITEM_PROGRESS);
        intentFilter.addAction(SyncTaskService.ACTION_SYNC_ITEM_RESULT);
        intentFilter.addAction(SyncTaskService.ACTION_SYNC_WORKOUTS_START);
        intentFilter.addAction(SyncTaskService.ACTION_SYNC_WORKOUTS_FINISH);
        intentFilter.addAction(SyncTaskService.ACTION_SYNC_ALREADY_SYNC);
        registerReceiver(broadcastReceiver, intentFilter, Context.RECEIVER_EXPORTED);

        this.localWorkoutAdapter.setExportCallBack(new Function1<Workout, Unit>() {

            @Override
            public Unit invoke(Workout workout) {
                invoke2(workout);
                return Unit.INSTANCE;
            }


            public void invoke2(Workout it2) {

                exportFit(it2);
            }
        });

        this.localWorkoutAdapter.setUploadCallBack(it2 -> {
            Long id = it2.getId();
            DeviceSyncHelper.syncWorkoutWithId(App.getContext(), id);
            return true;
        });

        initData();
    }

    private void initData() {
        if (App.getContext().isUserSignin()) {
            this.getLocalWorkoutAdapter().setList(Workout.getUnUploadWorkouts(App.getContext().getUserId()));
        }


    }

    public void exportFit(Workout workout) {
        File file = new File(workout.getFitPath());
        if (file.exists()) {
            String[] strArr = {"微信", Constants.SOURCE_QQ};
            AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
            builder.setTitle("请选择分享到哪个平台");
            builder.setItems(strArr, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    LocalWorkoutActivity.exportFit(LocalWorkoutActivity.this, file, dialogInterface, i);
                }
            });
            builder.show();
            return;
        }
        ToastUtils.showShort("上传失败，文件不存在", new Object[0]);
    }

    public static void exportFit(LocalWorkoutActivity this$0, File file, DialogInterface dialogInterface, int i) {
        if (i == 0) {
            ShareLogUtil.shareFile(this$0.context, file, ShareLogUtil.PACKAGE_NAME_WX);
        } else {
            ShareLogUtil.shareFile(this$0.context, file, ShareLogUtil.PACKAGE_NAME_QQ);
        }
    }

    public void onUpdate(long workoutId, double d) {
        this.localWorkoutAdapter.setUploading(workoutId, (int) d);
    }

    public void onFailed(long workoutId, String str) {
        this.localWorkoutAdapter.setUploadFailed(workoutId, str);
    }

    public void onSuccess(long workoutId) {
        this.localWorkoutAdapter.setUploaded(workoutId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(this.workoutSyncBroadcastReceiver);
    }

    public static class Companion {
        public Companion(DefaultConstructorMarker defaultConstructorMarker) {
            this();
        }

        private Companion() {
        }

        public void start(Context context) {

            context.startActivity(new Intent(context, LocalWorkoutActivity.class));
        }
    }
}
